package com.woniuxy.controller;


import com.woniuxy.demo.Result;
import com.woniuxy.dto.*;
import com.woniuxy.dto.commonDto.BaseDto;
import com.woniuxy.dto.goodsGeneralAndSmallAndBrand.GoodsGeneralDto;
import com.woniuxy.dto.goodsGeneralAndSmallAndBrand.GoodsSmallDto;
import com.woniuxy.dto.goodsGeneralAndSmallAndBrand.GoodsSmallerDto;
import com.woniuxy.enums.CodeEnum;
import com.woniuxy.exception.PawnUserException;
import com.woniuxy.param.AddCommodityParam;
import com.woniuxy.param.CommodityParam;
import com.woniuxy.service.*;
import com.woniuxy.util.ValidatedUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品详情
 * </p>
 *
 * @author zcx, ftl, gr, rhs
 * @since 2021-12-31
 */
@RestController
@RequestMapping("/commodity-table")
@Api(tags = "商品接口")
public class CommodityTableController {

    @Resource
    private CommodityTableService service;

    @Resource
    private ShopTableService sts;

    @Resource
    private GoodsPropgroupService goodsPropgroupService;

    @Resource
    private GoodsGeneralService ggs;

    @Resource
    private GoodsSmallService gss;

    @Resource
    private GoodsSmallerService gses;


    //查询三级类

    /**
     * 查询大类 输入品牌id
     *
     * @param generalId
     * @return
     */
    @ApiOperation("查询所有大类")
    @PostMapping("listGoodsGeneralDto.do/{generalId}")
    public Result listGoodsGeneralDto(@PathVariable Integer generalId) {
        List<GoodsGeneralDto> goodsGeneralDtos = ggs.listGeneralDtos(generalId);

        return Result.newInstance().setCode(200).setMsg("查询成功").setData(goodsGeneralDtos);
    }

    /**
     * 用大类id查询当下二级类
     *
     * @param goodsGeneralId
     * @return
     */
    @ApiOperation("用大类id查询当下二级类")
    @PostMapping("listGoodsSmallDto.do/{goodsGeneralId}")
    public Result listGoodsSmallDto(@PathVariable Integer goodsGeneralId) {
        List<GoodsSmallDto> goodsSmallDtos = gss.listGoodsSmallDto(goodsGeneralId);

        return Result.newInstance().setCode(200).setMsg("查询成功").setData(goodsSmallDtos);
    }

    /**
     * 用二类id查询当下三级类
     *
     * @param goodsSmallId
     * @return
     */
    @ApiOperation("用二类id查询当下三级类")
    @PostMapping("listGoodsSmallerDto.do/{goodsSmallId}")
    public Result listGoodsSmallerDto(@PathVariable Integer goodsSmallId) {
        List<GoodsSmallerDto> goodsSmallerDtos = gses.listGoodsSmallerDto(goodsSmallId);

        return Result.newInstance().setCode(200).setMsg("查询成功").setData(goodsSmallerDtos);
    }


    /**
     * 分页和模糊查询所有商品
     *
     * @param cp
     * @return
     */
    @ApiOperation("分页和模糊查询所有商品")
    @PostMapping("listCommodity.do")
    public Result listCommodity(@RequestBody CommodityParam cp) {
        BaseDto<CommodityDto> commodity = service.listCommodityByKey(cp);

        return Result.newInstance().setCode(200).setMsg("查询成功").setData(commodity);
    }

    /**
     * 查询所有门店
     *
     * @return
     */
    @ApiOperation("查询所有门店")
    @PostMapping("listShops.do")
    public Result listShops() {
        List<ShopDto> shopDtos = sts.listShops();
        return Result.newInstance().setCode(200).setMsg("查询成功").setData(shopDtos);
    }

    /**
     * 查询所有属性组及其下属性
     *
     * @param goodsSmallerId
     * @return
     */
    @ApiOperation("查询所有属性组及其下属性")
    @PostMapping("listGoodsPropGroups.do/{goodsSmallerId}")
    public Result listGoodsPropGroups(@PathVariable Integer goodsSmallerId) {
        List<PropGroupAndPropDto> propGroupAndPropDtos = goodsPropgroupService.listGoodsPropGroups(goodsSmallerId);
        return Result.newInstance().setCode(200).setMsg("查询成功").setData(propGroupAndPropDtos);
    }


    /**
     * 查询三级类所有所有属性
     *
     * @param goodsSmallerId
     * @return
     */
    @ApiOperation("查询三级类所有所有属性")
    @PostMapping("listGoodsProps.do/{goodsSmallerId}")
    public Result listGoodsProps(@PathVariable Integer goodsSmallerId) {
        List<GoodsPropDto> propGroupAndPropDtos = goodsPropgroupService.listGoodsProps(goodsSmallerId);
        return Result.newInstance().setCode(200).setMsg("查询成功").setData(propGroupAndPropDtos);
    }

    /**
     * 新增商品信息
     * @param addCommodityParam
     * @param result
     * @return
     */
    @ApiOperation("新增商品信息")
    @PostMapping("/addCommodityInfo")
    public Result addCommodityInfo(@RequestBody @Validated AddCommodityParam addCommodityParam, BindingResult result) {
        //参数验证
        ValidatedUtil.ValidatedException(result);

        return Result.newInstance().setCode(200).setMsg("添加成功")
                .setData(service.addCommodity(addCommodityParam));
    }

    /**
     * 详情展示
     *
     * @param commodityTableId
     * @return
     */
    @ApiOperation("详情展示")
    @PostMapping("/getCommodityInfo/{commodityTableId}")
    public Result getCommodityInfo(@PathVariable Integer commodityTableId) {
        CommodityDto propDto = service.getCommodityInfo(commodityTableId);

        return Result.newInstance().setCode(200).setMsg("查询成功")
                .setData(propDto);
    }



    /**
     * 批量删除商品(逻辑删除)
     *
     * @param ids
     * @return
     */
    @ApiOperation("批量删除商品(逻辑删除)")
    @PostMapping("/removeGoodsCommodityByIds.do")
    public Result removeGoodsCommodityByIds(@RequestBody List<Integer> ids) {
        if (service.removeGoodsCommodityByIds(ids)) {
            return Result.newInstance().setCode(200).setMsg("删除成功");
        }

        throw new PawnUserException(CodeEnum.COMMODITY_DELETE_FAIL_1.getMessage(), CodeEnum.COMMODITY_DELETE_FAIL_1.getCode());

    }
}

